﻿using DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FormatUtil
{
    public static class GpsTool
    {
        public static Gps HandleGps(byte[] buffer)
        {
            try
            {
                Gps gps = new Gps();
                gps.alarm = GwUtil.ToInt32(buffer, 0);
                gps.alarmDetail = GwUtil.analyzeAlarm(gps.alarm);
                gps.TheAlarm = GwUtil.GetAlarm(GwUtil.IntToHex(GwUtil.ToInt32(buffer, 0), 8));
                gps.status = GwUtil.ToInt32(buffer, 4);
                gps.TheStatus = GwUtil.GetStatus(GwUtil.IntToHex(GwUtil.ToInt32(buffer, 4), 8));
                gps.statusDetail = GwUtil.analyzeState(gps.status);
                gps.Latitude = GwUtil.ToInt32(buffer, 8) / 1000000.0;
                gps.Longitude = GwUtil.ToInt32(buffer, 12) / 1000000.0;
                gps.high = GwUtil.ToInt16(buffer, 16);
                gps.Speed = (int)(GwUtil.ToInt16(buffer, 18) / 10.0);
                gps.Direction = (int)GwUtil.ToInt16(buffer, 20);
                byte[] bs = new byte[6];
                Buffer.BlockCopy(buffer, 22, bs, 0, 6);
                string SgpsTime = GwUtil.BytesToHex(bs);
         
                gps.GpsTime = DateTime.ParseExact(SgpsTime, "yyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture);
                double timesc = Math.Abs((DateTime.Now - gps.GpsTime).TotalHours);
                if (timesc > 7 && timesc<9)
                {
                    gps.GpsTime = gps.GpsTime.AddHours(8);
                }
                gps.SgpsTime = gps.GpsTime.ToString("yyyy-MM-dd HH:mm:ss");
                AttachData attach = new AttachData();
               

                #region 附加数据解析
                if (buffer.Length > 28)
                {
                    bs = null;
                    bs = new byte[buffer.Length - 28];
                    Buffer.BlockCopy(buffer, 28, bs, 0, buffer.Length - 28);

                    while (bs.Length > 0)
                    {
                        if (bs.Length < 2) { break; }
                        int attachId = bs[0];
                        int attachLen = bs[1];
                        byte[] tmpbs = new byte[attachLen];
                        Buffer.BlockCopy(bs, 2, tmpbs, 0, attachLen);
                        string attachContent = GwUtil.BytesToHex(tmpbs);

                        switch (attachId)
                        {
                            case 0x01:
                                //里程，对应车上里程表读数
                                attach.Mileage = GwUtil.HexToInt(attachContent);
                                break;
                            //case 0x02:
                            //    //油量，对应车上油量表读数
                            //    attach.OilMass = GwUtil.HexToInt(attachContent);
                            //    break;
                            case 0xF1:
                                //扩展字段 3字节 状态（1字节）+数值（2字节）
                                int oilState = GwUtil.HexToInt(attachContent.Substring(0,2));
                                int oilHight = GwUtil.HexToInt(attachContent.Substring(2, 4));
                                attach.OilMass = oilHight;
                                attach.OilMassState = oilState;
                                break;
                            case 0x03:
                                //行驶记录功能获取的速度km/h
                                attach.RunSpeed = (int)(GwUtil.HexToInt(attachContent) / 10.0);
                                break;
                            //case 0x04:
                            //    //转鼓状态：首字节=正反转，1为正转，0为反转；后两字节表示转速，单位：100ms/圈
                            //    attach.Barrate = GwUtil.HexToInt(attachContent.Substring(0, 2));
                            //    attach.BarrateSpeed = GwUtil.HexToInt(attachContent.Substring(2));
                            //    break;
                            case 0x04:
                                attach.AlarmEvent = GwUtil.HexToInt(attachContent);
                                break;
                            //case 0x11:
                            //    //超速报警附加信息
                            //    attach.OverSpeedAlarmType = GwUtil.HexToInt(attachContent);
                            //    break;
                            case 0x11:
                                //超速报警附加信息
                                attach.OverSpeedAlarmType = GwUtil.HexToInt(attachContent.Substring(0, 2));
                                attach.OverSpeedAlarmID = GwUtil.HexToInt(attachContent.Substring(2)).ToString();
                                break;
                            case 0x12:
                                //进出区域/路线报警附加信息
                                attach.AreaAlarmType = GwUtil.HexToInt(attachContent.Substring(0, 2));
                                attach.AreaAlarmID = GwUtil.HexToInt(attachContent.Substring(2, 8)).ToString();
                                attach.AreaAlarmDirection = GwUtil.HexToInt(attachContent.Substring(10));
                                break;
                            case 0x13:
                                //路段行驶时间不足/过长报警附加信息
                                attach.RoadAlarmID = GwUtil.HexToInt(attachContent.Substring(0, 8)).ToString();
                                attach.RoadAlarmTime = GwUtil.HexToInt(attachContent.Substring(8, 4));
                                attach.RoadAlarmResult = GwUtil.HexToInt(attachContent.Substring(12));
                                break;

                            case 0x14:
                                attach.VideoAlarmFlag = GwUtil.GetVideoAlarm(attachContent);
                                break;
                            case 0x15:
                                attach.VideoPassLoseState = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x16:
                                attach.VideoPassShelterState = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x17:
                                attach.MemoryFailureState = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x18:
                                attach.AbnormalDrivingBehaviorAlarmDetail = GwUtil.GetAbnormalDrivingBehaviorAlarm(attachContent.Substring(0, 2));
                                attach.AbnormalDrivingBehaviorAlarmDetail.fatigueDegree = GwUtil.HexToInt(attachContent.Substring(2, 2));
                                break;

                            case 0x25:
                                attach.car_signal = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x2A:
                                attach.io_status = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x2B:
                                attach.analog = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x30:
                                attach.signal_strength = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x31:
                                attach.gnss_count = GwUtil.HexToInt(attachContent);
                                break;

                            case 0xE1:
                                //LBS 信息

                                break;
                            case 0xE2:
                                //IMEI 码
                                //attach.imei = GwUtil.HexToStr(attachContent);
                                break;
                            case 0xE3:
                                //版本号，ASCII 字符串
                                //attach.version = GwUtil.HexToStr(attachContent);
                                break;
                            case 0xE6:
                                //ICCID 字符串
                                //attach.iccid = GwUtil.HexToStr(attachContent);
                                break;
                            case 0xE7:
                                //报警状态
                                //attach.lockAlarm = GwUtil.HexToInt(attachContent);
                                break;
                            case 0xE8:
                                //开关状态
                                //attach.onOffState = GwUtil.HexToInt(attachContent);
                                break;
                            case 0xE9:
                                //设备电量，百分比
                                //attach.electricity = GwUtil.HexToInt(attachContent);
                                break;
                            case 0x51:
                                //n 路温度，

                                break;
                            case 0x58:
                                //n 路湿度，

                                break;
                            case 0x56:
                                //设备电量电压
                                //attach.voltage = GwUtil.HexToInt(attachContent.Substring(2, 2)) * 50;
                                //attach.electricity = GwUtil.HexToInt(attachContent.Substring(0,2));
                                break;
                            case 0xEA:
                                //G-sensor 三轴（X,Y,Z）加速度

                                break;

                            case 0xFE:
                                if (attachContent.Substring(0,2)== "E6")
                                {
                                    attachContent = attachContent.Substring(2);
                                    while (attachContent.Length>2)
                                    {
                                        string extendId = attachContent.Substring(0, 2);
                                        int extendDataLen = GwUtil.HexToInt(attachContent.Substring(2, 4));
                                
                                        switch (extendId)
                                        {
                                            case "02":
                                                //attach.electricity = GwUtil.HexToInt(attachContent.Substring(6, extendDataLen*2));
                                                break;
                                            case "07":
                                                break;
                                            case "20":
                                                break;
                                        }
                                        attachContent = attachContent.Substring(6 + extendDataLen * 2);
                                    }
                                 
                                }

                                break;




                            case 0xEB:
                                //悦心超声波油耗传感器
                                //attachContent = attachContent.Substring(8);
                                //attachContent = GwUtil.HexToStr(attachContent);
                                //double yhz = Convert.ToDouble(attachContent);
                                //gps.OilPercent = yhz;
                                break;

                            //case 0x25:
                            //    //新北斗 扩展车辆信号状态位
                            //    // ParseStatusXinBeidou(gps.TheStatus, attachContent);
                            //    break;

                            default:

                                break;
                        }
                        byte[] b = new byte[bs.Length - attachLen - 2];
                        Buffer.BlockCopy(bs, 2 + attachLen, b, 0, bs.Length - 2 - attachLen);
                        bs = null;
                        bs = b;
                    }
                }
                gps.Attach = attach;
                #endregion

                return gps;
            }
            catch
            {
                return null;
            }

        }
    }
}
